//logs.js
var util = require('../../utils/util.js')
const Dialog = require('../../dist/dialog/dialog');
var app = getApp()
// Page({
//  data: {
//    logs: []
//  },
//  onLoad: function () {
//    this.setData({
//      logs: (wx.getStorageSync('logs') || []).map(function (log) {
//        return util.formatTime(new Date(log))
//      })
//    })
//  }
// })

Page({
  data: {
    focus: false,
    inputValue: '',
    list:[]
  },
  bindButtonTap: function() {
    this.setData({
      focus: Date.now()
    })
  },
  bindKeyInput: function(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  bindReplaceInput: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    if(pos != -1){
      //光标在中间
      var left = e.detail.value.slice(0,pos)
      //计算光标的位置
      pos = left.replace(/11/g,'2').length
    }

    //直接返回对象，可以对输入进行过滤处理，同时可以控制光标的位置
    return {
      value: value.replace(/11/g,'2'),
      cursor: pos
    }

    //或者直接返回字符串,光标在最后边
    //return value.replace(/11/g,'2'),
  },
  bindHideKeyboard: function(e) {
    if (e.detail.value === '123') {
      //收起键盘
      wx.hideKeyboard()
    }
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var list = e.target.dataset.list;
    console.log(list)
    var imglist = list.split(',');
    console.log(imglist)
    wx.previewImage({
      current: imglist[current], // 当前显示图片的http链接  
      urls: imglist // 需要预览的图片http链接列表  
    })
  },
  onShow() {
    console.log(app.state)
    if (!app.state) {
      Dialog({
        title: '申请',
        message: '请完善用户信息',
        showCancelButton: true,
        selector: '#zan-dialog-test'
      }).then((res) => {
        wx.navigateTo({
          url: '../userInfo/userInfo',
        })
      }).catch(err => {
        wx.reLaunch({
          url: '../discovery/discovery'
        })
      })
    } else if (app.state==='1') {
      Dialog({
        title: '申请',
        message: '您提交申请,请等待审核',
        selector: '#zan-dialog-test'
      }).then((res) => {
        wx.reLaunch({
          url: '../discovery/discovery'
        })
      })
    } else if (app.state === '2'){
      var that = this;
      wx.request({
        url: app.ipaddress+'getFriendCircleList',
        method: 'GET',
        data: {},
        success: function (res) {
          console.log(res)
          var list = res.data.list;
          if (list == null) {
            var toastText = '获取数据失败' + res.data.errMsg;
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            });
          } else {
            console.log(list)
            that.setData({
              list: list
            });
          }
        }
      })
    }
  }
})